package cmpe451.group7.ourcity.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cmpe451.group7.ourcity.dao.BadgeDAO;
import cmpe451.group7.ourcity.dao.PlaceDAO;
import cmpe451.group7.ourcity.dao.UserDAO;
import cmpe451.group7.ourcity.entity.Badge;
import cmpe451.group7.ourcity.entity.Place;
import cmpe451.group7.ourcity.entity.Tag;
import cmpe451.group7.ourcity.entity.User;
import cmpe451.group7.ourcity.service.PlaceService;

@Service("placeService")
public class PlaceServiceImpl implements PlaceService, Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
	private PlaceDAO placeDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private BadgeDAO badgeDAO;
	
	

	public Place createPlace(String placeName, String country, String city,
			int likeCount, double latitude, double longitude,
			Date creationDate, List<Tag> tags, User user){
		
		
		
		
		
		
		List<Badge> placeBadges = badgeDAO.getPlaceBadges();
		
		int userPlacesCount = placeDAO.getNumberOfUserPlaces(user);
		
		Set<Badge> userBadges = user.getBadges();

		for(int i = 0; i < placeBadges.size(); i++){
			if(userPlacesCount + 1 == placeBadges.get(i).getBadgeCount()){
				if(!userBadges.contains(placeBadges.get(i))){
					userBadges.add(placeBadges.get(i));
					RequestContext context = RequestContext.getCurrentInstance();
					FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "You got badge",
							placeBadges.get(i).getBadgeName());
					FacesContext.getCurrentInstance().addMessage(null, msg);
				}
			}
		}
		
		userDAO.update(user);
		
		return placeDAO.insertNewPlace(placeName, country, city, likeCount, latitude, longitude, creationDate, tags, user);
		
	}
	
	
//	@Override
//	@Transactional
//	public List<Place> searchAndFillTags(String placeName, String country,
//			String city, double latitude, double longitude, Date fromDate,
//			Date toDate, List<Tag> tags) {
//		List<Place> places = placeDAO.findByRestrictions(placeName, country,
//				city, latitude, longitude, fromDate, toDate, tags);
//
//		if (places != null) {
//			for (Place p : places) {
//				placeDAO.initialize(p.getTags());
//			}
//		}
//
//		return places;
//	}

	@Override
	@Transactional
	public void likePlace(User u, Place p) {
		boolean liked = userDAO.likePlace(p, u);

		if (liked) {
			int oldValue = p.getLikeCount();
			p.setLikeCount(oldValue + 1);
			boolean isSaved = placeDAO.saveOrUpdate(p);

			if (!isSaved) {
				p.setLikeCount(oldValue);
			}
		}
	}

	@Override
	@Transactional
	public Place findAndFillTags(long placeID) {
		Place p = placeDAO.findByID(placeID);
		placeDAO.initialize(p.getTags());
		return p;
	}

	@Override
	@Transactional
	public void saveOrUpdate(Place place) {
		placeDAO.saveOrUpdate(place);
	}

	@Override
	@Transactional
	public Place findAndFillTagsAndMultimediaId(int placeID) {
		Place place = findAndFillTags(placeID);

		placeDAO.initialize(place.getMultimedia());

		return place;
	}

}
